<template>
  <div class="bottom">
    <div class="bottom-bar">
      <div class="check-content">
        <!-- 监听组件点击 @click.native -->
        <check-button
          class="check-button"
          :is-checked="isSelectAll"
          @click.native="checkClick"
        ></check-button>
        <span>全选</span>
      </div>

      <div class="price">合计:￥ {{ totalPrice }}</div>

      <div class="calculate" @click="calcClick">去结算:{{ checkLength }}</div>
    </div>
  </div>
</template>

<script>
import CheckButton from "components/content/checkButton/CheckButton.vue";
import { mapGetters } from "vuex";

export default {
  name: "CartBottomBar",
  components: {
    CheckButton,
  },
  computed: {
    ...mapGetters(["cartList"]),
    // 总价
    totalPrice() {
      return (
        // "￥" +
        this.cartList
          .filter((item) => {
            return item.checked;
          })
          .reduce((preValue, item) => {
            return preValue + item.price * item.count;
            // 保留两位小数
          }, 0)
          .toFixed(2)
      );
    },

    // 计算
    checkLength() {
      return this.cartList.filter((item) => item.checked).length;
    },

    isSelectAll() {
      if (this.cartList.length == 0) return false;

      // 使用find
      // return !this.cartList.find(item => !item.checked)

      // 普通方法
      for (let item of this.cartList) {
        if (!item.checked) {
          return false;
        }
      }
      return true;
    },
  },
  methods: {
    // 购物车全选
    checkClick() {
      // 全部选中
      if (this.isSelectAll) {
        this.cartList.forEach((item) => (item.checked = false));
        //部分或全部不选中
      } else {
        this.cartList.forEach((item) => (item.checked = true));
      }
      if (this.cartList.length == 0) {
        console.log("请选择了喜欢的商品");
      }
    },

    calcClick() {
      if (!this.isSelectAll) {
        this.$toast.show("请选择购买的商品", 2000);
      }

      this.$emit("calcClick",this.totalPrice)
    },
  },
};
</script>

<style scoped>
.bottom {
  width: 100%;
  position: fixed;
  bottom: 49px;
  background: url("../../../assets/img/regisetr/login2.jpg");
  background-size: cover;
}
.bottom-bar {
  line-height: 40px;
  height: 40px;

  /* background-color: #eee; */
  position: relative;
  display: flex;
  font-size: 14px;
}

.check-content {
  display: flex;
  align-items: center;
  margin-left: 10px;
  width: 60px;
}

.check-button {
  width: 20px;
  height: 20px;
  line-height: 20px;
  margin-right: 5px;
}

.price {
  margin-left: 20px;
  flex: 1;
}

.calculate {
  width: 90px;
  background-color: var(--color-tint);
  text-align: center;
  color: #fff;
}
</style>
